﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSP.MathLibrary;
using System.Data.SqlClient;
using System.Data;

namespace DataLayer
{
    public class ClimateDataManager
    {
#if remote
        private static string _connectionString = @"server = 127.0.0.1,1357; database = climate;uid = clidata; pwd=data_wo;Trusted_Connection=no";
#else
        private static string _connectionString = @"server = 192.168.116.101\sql2k; database = climate;uid = clidata; pwd=data_wo;Trusted_Connection=no";
#endif
    

        public static MeteoDataRaw GetList(int stationId, string paramName, DateTime dtBegin, DateTime dtEnd)
        {

            MeteoDataRaw mdr = new MeteoDataRaw();
            mdr.TimeStep = 3.0;

            List<ComplexNumber> result = new List<ComplexNumber>();

            using (SqlConnection con = new SqlConnection(_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("", con))
                {
                    cmd.CommandText = string.Format(@"select {0}, date_obs from tmc_sroki
                                        where station_id=@sid and date_obs between @dtBegin  and @dtEnd
                                        --and datepart(mm, date_obs)  in (11, 12, 1, 2, 3)
                                        and datepart(hh, date_obs) in (0,3,6,9,12,15,18,21)
                                        order by date_obs", paramName);
                    cmd.Parameters.AddWithValue("@sid", stationId);
                    cmd.Parameters.AddWithValue("@dtBegin", dtBegin);
                    cmd.Parameters.AddWithValue("@dtEnd", dtEnd);

                    con.Open();

                    IDataReader idr = cmd.ExecuteReader();
                    while (idr.Read())
                    {
                        if (idr[0] is DBNull)
                        {
                            mdr.SourceDataRow.Add(new ObservationPoint { DateTime = Convert.ToDateTime(idr[1]), Value=double.NaN });
                           // result.Add(result[result.Count - 1]);
                        }
                        else
                        {
                            mdr.SourceDataRow.Add(new ObservationPoint { DateTime = Convert.ToDateTime(idr[1]), Value = Convert.ToDouble(idr[0]) });
                            //result.Add(new ComplexNumber(Convert.ToDouble(idr[0]), 0));
                        }
                    }
                    con.Close();
                }
            }

            return mdr;
        }

        public static List<ObservationPoint>[] GetInersections(int statoinid, string paramName, int startYear, int endYear)
        {
            int numYears = (endYear - startYear);
            int numObservs = 365 * 8;

            DateTime dtBegin = new DateTime(startYear, 1, 1);
            DateTime dtEnd = new DateTime(endYear, 1, 1).AddDays(365);

            List<ObservationPoint>[] result = new List<ObservationPoint>[numYears];

            for (int i = 0; i < numObservs; i++)
            {

            }

            /*SELECT     DATE_OBS, TEMPDB
FROM         Tmc_Sroki
WHERE     (DATEPART(dy, DATE_OBS) = 2) AND (Station_ID = 4313181) AND (DATEPART(hh, DATE_OBS) = 3)
ORDER BY DATE_OBS*/
           
            /*SELECT     DATEPART(yy, DATE_OBS) AS yy, DATEPART(mm, DATE_OBS) AS mm, DATEPART(dd, DATE_OBS) AS dd, AVG(TEMPDB) AS Expr1
FROM         Tmc_Sroki
WHERE     (DATEPART(dy, DATE_OBS) = 60) AND (Station_ID = 4313181)
GROUP BY DATEPART(yy, DATE_OBS), DATEPART(mm, DATE_OBS), DATEPART(dd, DATE_OBS)
ORDER BY yy, mm, dd*/

            return result;
        }
    }
}
